home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
SNIP1292.ARJ
/
RG_SSORT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-27
|
1KB
|
44 lines
/*
** ssort() -- Fast, small, qsort()-compatible Shell sort
**
** by Ray Gardner, public domain 5/90
*/
#include <stddef.h>
void ssort (const void *base,
size_t nel,
size_t width,
int (*comp)(const void *, const void *))
{
size_t wnel, gap, wgap, i, j, k;
char *a, *b, tmp;
wnel = width * nel;
for (gap = 0; ++gap < nel;)
gap *= 3;
while ( gap /= 3 )
{
wgap = width * gap;
for (i = wgap; i < wnel; i += width)
{
for (j = i - wgap; ;j -= wgap)
{
a = j + (char *)base;
b = a + wgap;
if ( (*comp)(a, b) <= 0 )
break;
k = width;
do
{
tmp = *a;
*a++ = *b;
*b++ = tmp;
} while ( --k );
if (j < wgap)
break;
}
}
}
}